package qs.crm.modules.storage.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.storage.domain.Storage;
import qs.crm.modules.storage.repository.StorageRepository;
import qs.crm.modules.storage.service.StorageService;
import qs.crm.modules.storage.service.dto.StorageDto;
import qs.crm.modules.storage.service.dto.StorageQueryCriteria;
import qs.crm.modules.storage.service.mapstruct.StorageMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author tong
* @date 2023-06-04
**/
@Service
@RequiredArgsConstructor
public class StorageServiceImpl implements StorageService {

    private final StorageRepository storageRepository;
    private final StorageMapper storageMapper;

    @Override
    public Map<String,Object> queryAll(StorageQueryCriteria criteria, Pageable pageable){
        Page<Storage> page = storageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(storageMapper::toDto));
    }

    @Override
    public List<StorageDto> queryAll(StorageQueryCriteria criteria){
        return storageMapper.toDto(storageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StorageDto findById(Long stkId) {
        Storage storage = storageRepository.findById(stkId).orElseGet(Storage::new);
        ValidationUtil.isNull(storage.getStkId(),"Storage","stkId",stkId);
        return storageMapper.toDto(storage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StorageDto create(Storage resources) {
        return storageMapper.toDto(storageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Storage resources) {
        Storage storage = storageRepository.findById(resources.getStkId()).orElseGet(Storage::new);
        ValidationUtil.isNull( storage.getStkId(),"Storage","id",resources.getStkId());
        storage.copy(resources);
        storageRepository.save(storage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long stkId : ids) {
            storageRepository.deleteById(stkId);
        }
    }

    @Override
    public void download(List<StorageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StorageDto storage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("产品编号", storage.getStkProdId());
            map.put("仓库地址", storage.getStkWarehouse());
            map.put("仓库编号", storage.getStkWare());
            map.put("库存数量", storage.getStkCount());
            map.put("备注", storage.getStkMemo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}